import unittest
from unittest.mock import patch, MagicMock
# pyright: reportAttributeAccessIssue = false
from main import Api


class TestApi(unittest.TestCase):
    def setUp(self):
        self.api = Api()
        self.api.settings = self.api.getProjectSettings()

    @patch('paramiko.SSHClient')  # paramiko.SSHClient = mock_ssh = Mock()
    def test_createUserFolder_exists(self, mock_ssh):
        """测试用户目录已存在的情况"""
        mock_client = MagicMock()
        # 指定mock_ssh这个mock对象被调用时返回另一个mock对象，即mock_ssh.return_value = mock_ssh() = Mock()()= mock_client
        mock_ssh.return_value = mock_client

        # 模拟目录已存在
        mock_client.exec_command.return_value = (
            None,
            MagicMock(read=MagicMock(return_value=b"exists")),
            MagicMock()
        )

        result = self.api.createUserFolder()
        self.assertTrue(result['suc'])
        self.assertIn('已存在', result['msg'])

    def test_getLinuxUserName(self):
        """测试获取Linux用户名"""
        username = self.api.getLinuxUserName()
        self.assertEqual(username, 'root')


if __name__ == '__main__':
    unittest.main()
